package com.stu.day15;

import java.io.*;

/**
 * read(),write(int len)
 *
 * @author zy
 * @date 2025/2/13 15:23
 */
public class IODemo {
    public static void main(String[] args) {
        InputStream in = null;
        OutputStream out = null;

        try {
            in = new FileInputStream("src/com/stu/day15/test1.txt");
            out = new FileOutputStream("src/com/stu/day15/test1_副本.txt");

            while (true) {
                int len = in.read();  // 此方式读写交互次数太多,内容多的话非常慢
                if (len == -1) break;
                out.write(len);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //无论是否出现异常,都会执行,释放资源.
            //关闭流
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
